/*
 * bugs_edit.java
 *
 * Created on 15.12.2008, 1:17:30
 */
 
package buggywar;

import Classes.Bug;
import Classes.Component;
import Classes.Priority;
import Classes.Project;
import Classes.User;
import Classes.Version;
import SessionBeans.BugFacadeLocal;
import SessionBeans.ComponentFacadeLocal;
import SessionBeans.PriorityFacadeLocal;
import SessionBeans.ProjectFacadeLocal;
import SessionBeans.UserFacadeLocal;
import SessionBeans.VersionFacadeLocal;
import com.sun.data.provider.impl.CachedRowSetDataProvider;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.DropDown;
import com.sun.webui.jsf.component.StaticText;
import com.sun.webui.jsf.component.TextArea;
import com.sun.webui.jsf.component.TextField;
import com.sun.webui.jsf.model.Option;
import com.sun.webui.jsf.model.SingleSelectOptionsList;
import javax.ejb.EJB;
import javax.faces.FacesException;
import javax.faces.convert.IntegerConverter;

/**
 * <p>Page bean that corresponds to a similarly named JSP page.  This
 * class contains component definitions (and initialization code) for
 * all components that you have defined on this page, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @author Tomas
 */
public class bugs_edit extends AbstractPageBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">

    /**
     * <p>Automatically managed component initialization.  <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        versionDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.versionRowSet}"));
        projectDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.projectRowSet}"));
        componentDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.componentRowSet}"));
        priorityDataProvider.setCachedRowSet((javax.sql.rowset.CachedRowSet) getValue("#{SessionBean1.priorityRowSet}"));
    }

    // </editor-fold>

    private int bugEditIdentifier;
    private Bug bug;
    
    @EJB
    private BugFacadeLocal bugFacadeLocal;
    @EJB
    private UserFacadeLocal userFacadeLocal;
    private StaticText bugIdStatic1 = new StaticText();

    public StaticText getBugIdStatic1() {
        return bugIdStatic1;
    }

    public void setBugIdStatic1(StaticText st) {
        this.bugIdStatic1 = st;
    }
    private SingleSelectOptionsList stateDDDefaultOptions = new SingleSelectOptionsList();

    public SingleSelectOptionsList getStateDDDefaultOptions() {
        return stateDDDefaultOptions;
    }

    public void setStateDDDefaultOptions(SingleSelectOptionsList ssol) {
        this.stateDDDefaultOptions = ssol;
    }
    private TextField nameText = new TextField();

    public TextField getNameText() {
        return nameText;
    }

    public void setNameText(TextField tf) {
        this.nameText = tf;
    }
    private TextArea descriptionTextArea = new TextArea();

    public TextArea getDescriptionTextArea() {
        return descriptionTextArea;
    }

    public void setDescriptionTextArea(TextArea ta) {
        this.descriptionTextArea = ta;
    }
    private TextField keywordsText = new TextField();

    public TextField getKeywordsText() {
        return keywordsText;
    }

    public void setKeywordsText(TextField tf) {
        this.keywordsText = tf;
    }
    private StaticText addTimeStatic = new StaticText();

    public StaticText getAddTimeStatic() {
        return addTimeStatic;
    }

    public void setAddTimeStatic(StaticText st) {
        this.addTimeStatic = st;
    }
    private DropDown projectsDD = new DropDown();

    public DropDown getProjectsDD() {
        return projectsDD;
    }

    public void setProjectsDD(DropDown dd) {
        this.projectsDD = dd;
    }
    private DropDown versionsDD = new DropDown();

    public DropDown getVersionsDD() {
        return versionsDD;
    }

    public void setVersionsDD(DropDown dd) {
        this.versionsDD = dd;
    }
    private DropDown componentDD = new DropDown();

    public DropDown getComponentDD() {
        return componentDD;
    }

    public void setComponentDD(DropDown dd) {
        this.componentDD = dd;
    }
    private DropDown priorityDD = new DropDown();

    public DropDown getPriorityDD() {
        return priorityDD;
    }

    public void setPriorityDD(DropDown dd) {
        this.priorityDD = dd;
    }
    private DropDown stateDD = new DropDown();

    public DropDown getStateDD() {
        return stateDD;
    }

    public void setStateDD(DropDown dd) {
        this.stateDD = dd;
    }
    private StaticText ownerStatic = new StaticText();

    public StaticText getOwnerStatic() {
        return ownerStatic;
    }

    public void setOwnerStatic(StaticText st) {
        this.ownerStatic = st;
    }
    private TextField emailText = new TextField();

    public TextField getEmailText() {
        return emailText;
    }

    public void setEmailText(TextField tf) {
        this.emailText = tf;
    }
    private CachedRowSetDataProvider versionDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getVersionDataProvider() {
        return versionDataProvider;
    }

    public void setVersionDataProvider(CachedRowSetDataProvider crsdp) {
        this.versionDataProvider = crsdp;
    }
    private IntegerConverter versionsDDConverter = new IntegerConverter();

    public IntegerConverter getVersionsDDConverter() {
        return versionsDDConverter;
    }

    public void setVersionsDDConverter(IntegerConverter ic) {
        this.versionsDDConverter = ic;
    }
    private CachedRowSetDataProvider projectDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getProjectDataProvider() {
        return projectDataProvider;
    }

    public void setProjectDataProvider(CachedRowSetDataProvider crsdp) {
        this.projectDataProvider = crsdp;
    }
    private IntegerConverter projectsDDConverter = new IntegerConverter();

    public IntegerConverter getProjectsDDConverter() {
        return projectsDDConverter;
    }

    public void setProjectsDDConverter(IntegerConverter ic) {
        this.projectsDDConverter = ic;
    }
    private CachedRowSetDataProvider componentDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getComponentDataProvider() {
        return componentDataProvider;
    }

    public void setComponentDataProvider(CachedRowSetDataProvider crsdp) {
        this.componentDataProvider = crsdp;
    }
    private IntegerConverter componentDDConverter = new IntegerConverter();

    public IntegerConverter getComponentDDConverter() {
        return componentDDConverter;
    }

    public void setComponentDDConverter(IntegerConverter ic) {
        this.componentDDConverter = ic;
    }
    private CachedRowSetDataProvider priorityDataProvider = new CachedRowSetDataProvider();

    public CachedRowSetDataProvider getPriorityDataProvider() {
        return priorityDataProvider;
    }

    public void setPriorityDataProvider(CachedRowSetDataProvider crsdp) {
        this.priorityDataProvider = crsdp;
    }
    private IntegerConverter priorityDDConverter = new IntegerConverter();

    public IntegerConverter getPriorityDDConverter() {
        return priorityDDConverter;
    }

    public void setPriorityDDConverter(IntegerConverter ic) {
        this.priorityDDConverter = ic;
    }
    private Button confirmEdit = new Button();

    public Button getConfirmEdit() {
        return confirmEdit;
    }

    public void setConfirmEdit(Button b) {
        this.confirmEdit = b;
    }
    
    /**
     * <p>Construct a new Page bean instance.</p>
     */
    public bugs_edit() {
        bugEditIdentifier = bugs_add.getBugEditId();
    }

    /**
     * <p>Callback method that is called whenever a page is navigated to,
     * either directly via a URL, or indirectly via page navigation.
     * Customize this method to acquire resources that will be needed
     * for event handlers and lifecycle methods, whether or not this
     * page is performing post back processing.</p>
     * 
     * <p>Note that, if the current request is a postback, the property
     * values of the components do <strong>not</strong> represent any
     * values submitted with this request.  Instead, they represent the
     * property values that were saved for this view when it was rendered.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        // <editor-fold defaultstate="collapsed" desc="Managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("bugs_edit Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
        

        for (Bug buggy : bugFacadeLocal.findAll()) {
            if (buggy.getBugId() == bugEditIdentifier) {
                bug = buggy;
            }
        }

        String user = null;
        String state = bug.getState();

        for (User u : userFacadeLocal.findAll()) {
            if (u.getUserId() == bug.getUserId()) {
                user = u.getUserName();
            }
        }
        
        bugIdStatic1.setText(bug.getBugId());
        nameText.setText(bug.getBugTitle());
        descriptionTextArea.setText(bug.getDescription());
        keywordsText.setText(bug.getKeywords());
        addTimeStatic.setText(bug.getAddTime());
        projectsDD.setSelected(bug.getProjectId());
        versionsDD.setSelected(bug.getVersionId());
        componentDD.setSelected(bug.getComponentId());
        priorityDD.setSelected(bug.getPriorityId());
        
        Option[] options = new Option[5];
        options[0] = new Option(1, "NEW");
        options[1] = new Option(2, "OPENED");
        options[2] = new Option(3, "ASSIGNED");
        options[3] = new Option(4, "FIXED");
        options[4] = new Option(5, "INVALID");
        stateDD.setItems(options);

        if (state.equals("NEW")) {
            stateDD.setSelected(1);
        }
        if (state.equals("OPENED")) {
            stateDD.setSelected(2);
        }
        if (state.equals("ASSIGNED")) {
            stateDD.setSelected(3);
        }
        if (state.equals("FIXED")) {
            stateDD.setSelected(4);
        }
        if (state.equals("INVALID")) {
            stateDD.setSelected(5);
        }
        
        ownerStatic.setText(user);
        emailText.setText(bug.getUserEmail());
        
    }

    /**
     * <p>Callback method that is called after the component tree has been
     * restored, but before any event processing takes place.  This method
     * will <strong>only</strong> be called on a postback request that
     * is processing a form submit.  Customize this method to allocate
     * resources that will be required in your event handlers.</p>
     */
    @Override
    public void preprocess() {
    }

    /**
     * <p>Callback method that is called just before rendering takes place.
     * This method will <strong>only</strong> be called for the page that
     * will actually be rendered (and not, for example, on a page that
     * handled a postback and then navigated to a different page).  Customize
     * this method to allocate resources that will be required for rendering
     * this page.</p>
     */
    @Override
    public void prerender() {
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called (regardless of whether
     * or not this was the page that was actually rendered).  Customize this
     * method to release resources acquired in the <code>init()</code>,
     * <code>preprocess()</code>, or <code>prerender()</code> methods (or
     * acquired during execution of an event handler).</p>
     */
    @Override
    public void destroy() {
        versionDataProvider.close();
        projectDataProvider.close();
        componentDataProvider.close();
        priorityDataProvider.close();
    }

    public String confirmEdit_action() {
        
        bug.setBugTitle(nameText.getValue().toString().trim());
        bug.setDescription(descriptionTextArea.getValue().toString().trim());
        bug.setKeywords(keywordsText.getValue().toString().trim());
        bug.setUserEmail(emailText.getValue().toString().trim());
        bug.setUserId(bug.getUserId());
        bug.setProjectId(Integer.parseInt(projectsDD.getSelected().toString()));
        bug.setComponentId(Integer.parseInt(componentDD.getSelected().toString()));
        bug.setPriorityId(Integer.parseInt(priorityDD.getSelected().toString()));
        bug.setVersionId(Integer.parseInt(versionsDD.getSelected().toString()));
        bug.setAddTime(bug.getAddTime());

        if (Integer.parseInt(stateDD.getSelected().toString()) == 1) {
            bug.setState("NEW");
        }
        if (Integer.parseInt(stateDD.getSelected().toString()) == 2) {
            bug.setState("OPENED");
        }
        if (Integer.parseInt(stateDD.getSelected().toString()) == 3) {
            bug.setState("ASSIGNED");
        }
        if (Integer.parseInt(stateDD.getSelected().toString()) == 4) {
            bug.setState("FIXED");
        }
        if (Integer.parseInt(stateDD.getSelected().toString()) == 5) {
            bug.setState("INVALID");
        }

        try {
            bugFacadeLocal.edit(bug);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.print("CHYBA: " + bug.getBugId() + " NEBOLA UPRAVNA V DB");
        }
        
        System.out.print("BUG: " + bug.getBugId() + " BOLA UPRAVENY");        
        return "bug_updated";
    }

    public String back_action() {
        return "backTobugs";
    }
    
}

